package com.marvels.approve.process.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.marvels.approve.consum.ApproveConsumerProductApi;
import com.marvels.approve.loan.dto.McptChannelDto;
import com.marvels.approve.process.dao.IFaceSigConfiguractionDao;
import com.marvels.approve.process.dao.ITimeConfHistoryDao;
import com.marvels.approve.process.dto.FaceScreenConditionDTO;
import com.marvels.approve.process.dto.FaceSigConfigurationDTO;
import com.marvels.approve.process.dto.SysUserDTO;
import com.marvels.approve.process.dto.TimeConfHistoryDTO;
import com.marvels.approve.process.service.IFaceSigAppointmentService;
import com.marvels.approve.process.service.IFaceSigConfigurationService;
import com.marvels.approve.process.service.ITimeConfHistoryService;
import com.marvels.common.constants.PageConstant;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;

@Service
public class TimeConfHistoryServiceImpl implements ITimeConfHistoryService {

	@Autowired
	private IFaceSigAppointmentService  faceSigAppointmentService;
	
	@Autowired
	private ITimeConfHistoryDao timeConfHistoryDao;

	@Autowired
	private IFaceSigConfiguractionDao faceSigConfiguractionDao;
	
	@Autowired
	private IFaceSigConfigurationService  faceSigConfigurationService;
	
	@Autowired
	private ApproveConsumerProductApi consumerProductApi;
	/**
	 * 
	 * 分頁查询参数配置明细
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page<TimeConfHistoryDTO> selectFaceSigConfiguractionPage(Integer pageNo,Integer pageSize){
			if (pageNo <= 0) {
				pageNo = PageConstant.CURRPAGE;
			}
			if (pageSize <= 0) {
				pageSize = PageConstant.PAGESIZE;
			}
		PageHelper.startPage(pageNo, pageSize);
		Page<TimeConfHistoryDTO> list = timeConfHistoryDao.selectFaceSigConfAndTimeconfPage(pageNo, pageSize);
		return list;
	}
	
	@Transactional(timeout = 30 ,rollbackFor=Exception.class)
	@Override
	public int insertTimeConfHistory(TimeConfHistoryDTO timeConfHistoryDTO) throws MarvelsBusinessException {
		UserDTO user = CacheUtils.getCacheUser();
		timeConfHistoryDTO.setCreateBy(user.getUserCode());
		timeConfHistoryDTO.setUpdateBy(user.getUserCode());
		return timeConfHistoryDao.insertTimeConfHistory(timeConfHistoryDTO);
	}

	@Override
	public TimeConfHistoryDTO getTimeConfDetailed(long id,long facesigId) throws MarvelsBusinessException {
		return timeConfHistoryDao.selectTimeConfDetailed(id,facesigId);
	}
	
	@Transactional(timeout = 30 ,rollbackFor=Exception.class)
	@Override
	public int updateTimeConfHistory(TimeConfHistoryDTO timeConfHistoryDTO) {
		return timeConfHistoryDao.updateTimeConfHistory(timeConfHistoryDTO);
	}
	
	@Transactional(timeout = 30 ,rollbackFor=Exception.class)
	@Override
	public int addTimeConfHistoryAndTimeConf(TimeConfHistoryDTO timeConfHistoryDTO) throws MarvelsBusinessException {
		int count = 0;
		try {
			UserDTO user = CacheUtils.getCacheUser();  //从缓存中读取到用户
			// 生效时间（止）：不输入就默认到永久；
			 if(timeConfHistoryDTO.getEndTakeeffectTime() == null || "".equals(timeConfHistoryDTO.getEndTakeeffectTime())){
				 	timeConfHistoryDTO.setEndTakeeffectTime(null);
			 }
			 timeConfHistoryDTO.setCreateBy(user.getUserCode());
			 timeConfHistoryDTO.setUpdateBy(user.getUserCode());
			 //校验有效性
			count = timeConfHistoryDao.insertTimeConfHistory(timeConfHistoryDTO);
			if (count < 1) {
				throw new MarvelsBusinessException("更新配置参数失败" + timeConfHistoryDTO.toString());
			}
			if (timeConfHistoryDTO.getFaceSigConfigurationList().size() <= 0) {
				throw new MarvelsBusinessException("更新参数明细失败" + timeConfHistoryDTO.toString());
			}
			 //新建多个配置参数明細
			 for (FaceSigConfigurationDTO faceSigTimeConf :  timeConfHistoryDTO.getFaceSigConfigurationList()) {
				 //面签人员数量：不输入就默认取所有面签角色的数量
				 if(faceSigTimeConf.getFacesigNumber() == null){
					 List<SysUserDTO> users = faceSigAppointmentService.selectFaceSignUsers();
					 faceSigTimeConf.setFacesigNumber(users.size());
				 } 
				 //版本ID
				 faceSigTimeConf.setTimeConfHistoryId(timeConfHistoryDTO.getId());
				 //面签人数：不输入就默认取面签人员数量的值，输入了优先取输入的人数
				 if(faceSigTimeConf.getFacesigCount() == null){
					 faceSigTimeConf.setFacesigCount(faceSigTimeConf.getFacesigNumber());
				 }
				 faceSigTimeConf.setCreateBy(user.getUserCode());
				 faceSigTimeConf.setUpdateBy(user.getUserCode());
				 count =  faceSigConfiguractionDao.insertFaceSigAppointment(faceSigTimeConf);
				 if (count <= 0) {
					 throw new MarvelsBusinessException("更新配置参数明细是失败" +  faceSigTimeConf.toString());
				 }
			 }
			MarvelsLogUtil.getInstance().info("更新参数更新条数" + count);
		} catch (MarvelsBusinessException e) {
			throw e;
		}
		return count;
	}
	
	
	/**
	 * 通过版本ID更新参数配置
	 */
	@Transactional(timeout = 30 ,rollbackFor=Exception.class)
	@Override
	public int updateFaceSigConfAndTimeconf(TimeConfHistoryDTO timeConfHistoryDTO) throws MarvelsBusinessException {
		int count = 0;
		try {
			UserDTO user = CacheUtils.getCacheUser();  //从缓存中读取到用户
			 timeConfHistoryDTO.setCreateBy(user.getUserCode());
			 timeConfHistoryDTO.setUpdateBy(user.getUserCode());
			 timeConfHistoryDTO.getFaceSigConfigurationDTO().setUpdateBy(user.getUserCode());
			// 通过ID修改参数
			count = faceSigConfiguractionDao.updateFaceSigAppointment(timeConfHistoryDTO.getFaceSigConfigurationDTO());
			if (count < 1) {
				throw new MarvelsBusinessException("修改配置参数明细失败{}" + timeConfHistoryDTO.getFaceSigConfigurationDTO().toString());
			}
			// 通过版本ID修改参数明细
			count = timeConfHistoryDao.updateTimeConfHistory(timeConfHistoryDTO);
			if (count < 1) {
				throw new MarvelsBusinessException("修改配置参数失败" + timeConfHistoryDTO.toString());
			}
		} catch (MarvelsBusinessException e) {
			throw e;
		}
		return count;
	}
	@Override
	public TimeConfHistoryDTO selectKyTimeId(long id) throws MarvelsBusinessException {
		if(id == 0){
			throw new MarvelsBusinessException("版本ID为空");
		}
		return timeConfHistoryDao.selectKyTimeId(id);
	}

	@Override
	public FaceScreenConditionDTO getFaceScreenCondition() throws MarvelsBusinessException {
		FaceScreenConditionDTO dto=new FaceScreenConditionDTO();
		dto.setCaseNodes(faceSigConfigurationService.selectCaseNode());
		dto.setUserList(faceSigAppointmentService.selectFaceSignUsers());
		dto.setChannelList(consumerProductApi.mcptChannelAllInfo(new McptChannelDto()));
		return dto;
	}
}
